

struct ListNode {
      int val;
     ListNode *next;
     ListNode() : val(0), next(nullptr) {}
     ListNode(int x) : val(x), next(nullptr) {}
     ListNode(int x, ListNode *next) : val(x), next(next) {}
 };

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* start_1 = l1;
        ListNode* start_2 = l2;
        int next = 0;
        ListNode* ret = new(ListNode);
        ListNode* newhead = ret;
        while(start_1&&start_2)
        {
            ListNode* tmp = new(ListNode);
            ret -> next = tmp;
            ret = ret->next;
            tmp->val = (start_1->val)+(start_2->val)+next;
            if(tmp->val>9)
            {
                tmp->val-=10;
                next = 1;
            }
            else
            {
                next = 0;
            }
            start_1 = start_1->next;
            start_2 = start_2->next;
        }
        while(start_1)
        {
            ListNode* tmp = new(ListNode);
            ret -> next = tmp;
            ret = ret->next;
            tmp->val = (start_1->val) + next;
            if(tmp->val>9)
            {
                tmp->val-=10;
                next = 1;
            }
            else
            {
                next = 0;
            }
            start_1 = start_1->next;
        }
        while(start_2)
        {
            ListNode* tmp = new(ListNode);
            ret -> next = tmp;
            ret = ret->next;
            tmp->val = (start_2->val) + next;
            if(tmp->val>9)
            {
                tmp->val-=10;
                next = 1;
            }
            else
            {
                next = 0;
            }
            start_2 = start_2->next;
        }
        if(next==1)
        {
            ListNode* tmp = new(ListNode);
            ret -> next = tmp;
            ret = ret->next;
            tmp->val = 1;
        }

        return newhead->next;
    }
};